﻿using System;
using System.Collections.Concurrent;
using Iot.Shared.Model;

namespace Iot.AlarmDispose;

/// <summary>
///     告警接收接口 所有告警数据接收接口
/// </summary>
public abstract class AlertReceive : IDisposable
{
    private static readonly ConcurrentDictionary<AlertReceive, AlertReceive> AlertReceives = [];

    protected AlertReceive()
    {
        AlertReceives.TryAdd(this, this);
    }

    public void Dispose()
    {
        if (AlertReceives.TryRemove(this, out _)) GC.SuppressFinalize(this);
    }

    public static void Handle(SwitchBusModel data)
    {
        foreach (var item in AlertReceives) item.Value.SwitchHandle(data);
    }


    /// <summary>
    ///     实时数据更新
    /// </summary>
    /// <param name="data"></param>
    protected abstract void SwitchHandle(SwitchBusModel data);
}